package com.ochnas.volunteersignup.ui;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;

import com.ochnas.volunteersignup.domain.User;
import com.ochnas.volunteersignup.service.UserService;
import static com.ochnas.volunteersignup.ui.SharedConstants.*;


public class LoginController {

	private ApplicationService applicationService;
	private UserService userService;
	private User user;
	private boolean loggedIn;
	private String pwd1;
	private String pwd2;
	
	public LoginController() {
		super();
		user = new User();
		loggedIn = false;
	}
	
	public String newLogin() {
		user = new User();
		return "login";
	}

	public String login () {
		
		boolean isValid = true;
		if(user.getEmailAddress() == null || "".equals(user.getEmailAddress())){
			   applicationService.addErrorMessage("loginForm:emailAddress","Please enter an email address.");
			   isValid = false;
		}
		if (isValid) {
			if(!EMAIL_ADDRESS_PATTERN.matcher(user.getEmailAddress()).matches()) {
				applicationService.addErrorMessage("loginForm:emailAddress","Please enter a valid email address.");
				   isValid = false;
			}
		}
		
		if(user.getPassword() == null || "".equals(user.getPassword())){
			   applicationService.addErrorMessage("loginForm:password","Please enter a password.");
			   isValid = false;
		}

	   if (isValid)  {
		   try {
			user = userService.loginUserAccount(user.getEmailAddress(), user.getPassword());
			loggedIn = true;
		   } catch (NoResultException e) {
			   applicationService.addErrorMessage("loginForm:summary", e.getMessage());
			   return null;
		   }
	   } else {
		return null;
	   }
	
		return "home";
	}
	
	public String logout() {
		user = new User();
		loggedIn = false;
		applicationService.addErrorMessage("loginForm:summary","You have logged out.");
		return "login";
	}
	
	public String newAccount () {
		user = new User();
		return "newAccount";
	}
	
	public String saveAccount () {
		
		boolean isValid = true;

		// validate if email address field is empty
		if(user.getEmailAddress() == null || "".equals(user.getEmailAddress())){
			   applicationService.addErrorMessage("newAccountForm:emailAddress","Please enter an email address.");
			   isValid = false;
		}
		// validate if email address field match email patterns
		if (isValid) {
			if(!EMAIL_ADDRESS_PATTERN.matcher(user.getEmailAddress()).matches()) {
				applicationService.addErrorMessage("newAccountForm:emailAddress","Please enter a valid email address.");
				   isValid = false;
			}
		}
		boolean passwordsOkay = true;
		// validate if password field is empty
		if(pwd1 == null || "".equals(pwd1)){
			   applicationService.addErrorMessage("newAccountForm:password1","Please enter a password.");
			   isValid = false;
			   passwordsOkay = false;
		} else {
			if(!PASSWORD_PATTERN.matcher(pwd1).matches()) {
				applicationService.addErrorMessage("newAccountForm:password1",
				"Password lenght must be between 6 to 12 alphanumeric, including special characters.");
				   isValid = false;
				   passwordsOkay = false;
			} 
		}
		if(pwd2 == null || "".equals(pwd2)){
			   applicationService.addErrorMessage("newAccountForm:password2","Please enter a password.");
			   isValid = false;
			   passwordsOkay = false;
		} else {
			if(!PASSWORD_PATTERN.matcher(pwd2).matches()) {
				applicationService.addErrorMessage("newAccountForm:password2",
				"Password lenght must be between 6 to 12 alphanumeric, including special characters.");
				   isValid = false;
				   passwordsOkay = false;
			}
		}
		// validate if passwords entered match
		if (passwordsOkay) {
			if(!pwd1.equals(pwd2)) {
				applicationService.addErrorMessage("newAccountForm:password1",
				"Passwords do not match.");
				applicationService.addErrorMessage("newAccountForm:password2",
				"Passwords do not match.");
				   isValid = false;
				   pwd1 = "";
				   pwd2 = "";
			}
		}
		
		// validate if first name field is empty
		if(user.getFirstName() == null || "".equals(user.getFirstName())){
			   applicationService.addErrorMessage("newAccountForm:firstName","Please enter first name.");
			   isValid = false;
		}
		// validate if last name field is empty
		if(user.getLastName() == null || "".equals(user.getLastName())){
			   applicationService.addErrorMessage("newAccountForm:lastName","Please enter last name.");
			   isValid = false;
		}
		
		// validate if phone number field is empty
		if(user.getContactPhoneNumber() == null || "".equals(user.getContactPhoneNumber())){
			   applicationService.addErrorMessage("newAccountForm:contactPhoneNumber","Please enter phone number.");
			   isValid = false;
		} else {
			if(!PHONE_NUMBER_PATTERN.matcher(user.getContactPhoneNumber()).matches()) {
				applicationService.addErrorMessage("newAccountForm:contactPhoneNumber",
								"Phone Number Must Be Entered As: 7632211234 ");
				   isValid = false;
			}
		}
		
	   	if (isValid)  {
		   try {
			   user.setPassword(pwd1);
			   user = userService.createUserAccount(user.getEmailAddress(), user.getPassword(), user.getFirstName(),
					   					user.getLastName(), user.getContactPhoneNumber());
		   } catch (NonUniqueResultException e) {
				applicationService.addErrorMessage("newAccountForm:summary", e.getMessage());
				return null;
		   }
	    } else {
			return null;
		   }
	   	
	   	user = new User();
	   	applicationService.addErrorMessage("loginForm:summary", "You successfully created an Ochnas account.  You can now log in.");
		return "login";
	} // end of saveAccount method	
	
	public ApplicationService getApplicationService() {
		return applicationService;
	}
	
	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public boolean isLoggedIn() {
		return loggedIn;
	}

	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}
	public String getPwd1() {
		return pwd1;
	}

	public void setPwd1(String pwd1) {
		this.pwd1 = pwd1;
	}

	public String getPwd2() {
		return pwd2;
	}

	public void setPwd2(String pwd2) {
		this.pwd2 = pwd2;
	}
}
